﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PersonaLibrary.View.Host
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : IView
	{
		private static double GalleryScale
		{
			get
			{
				return Properties.Settings.Default.WorkTableScale;
			}
			set
			{
				Properties.Settings.Default.WorkTableScale = value;
			}
		}
		readonly System.Timers.Timer _saveTimer = new System.Timers.Timer();
		
		public MainWindow()
		{
			ConnectionTest connectionTest = new ConnectionTest();
			connectionTest.ShowDialog();

			InitializeComponent();
			NameScope.SetNameScope(commandsMenu, NameScope.GetNameScope(this));
			NameScope.SetNameScope(filtersMenu, NameScope.GetNameScope(this));

			Keyboard.AddKeyDownHandler(this, OnKeyDownHandler);
		}

		private void OnKeyDownHandler(object sender, KeyEventArgs e)
		{
			if(e.Key == Key.F && Keyboard.Modifiers == ModifierKeys.Control)
			{
				Keyboard.Focus(filterBox);
				filterBox.SelectAll();
			}
		}

		private void Button1_Click(object sender, RoutedEventArgs e)
		{
			popup1.IsOpen = true;
		}

		private void ListBox_Loaded(object sender, RoutedEventArgs e)
		{
			EventManager.RegisterClassHandler(typeof(ListBox), ListBox.MouseWheelEvent, new RoutedEventHandler(Window_MouseWheel), true);
			_saveTimer.Interval = 5000;
			_saveTimer.Elapsed += saveTimer_Elapsed;
		}

		void saveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			_saveTimer.Stop();
			Properties.Settings.Default.WorkTableScale = GalleryScale;
			Properties.Settings.Default.Save();
		}

		private void Window_MouseWheel(object sender, RoutedEventArgs e)
		{
			if (Keyboard.IsKeyDown(Key.RightCtrl) || Keyboard.IsKeyDown(Key.LeftCtrl))
			{
				GalleryScale += ((MouseWheelEventArgs)e).Delta * .0001;
				((ScaleTransform)FindResource("scale")).ScaleX = GalleryScale;
				((ScaleTransform)FindResource("scale")).ScaleY = GalleryScale;
				_saveTimer.Start();
			}
		}

		private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
		{
			((ScaleTransform)FindResource("scale")).ScaleX = GalleryScale;
			((ScaleTransform)FindResource("scale")).ScaleY = GalleryScale;
		}

		private void filterBox_TextChanged(object sender, TextChangedEventArgs e)
		{
			//listBox.Items.Filter = delegate(object arg)
			//                        {
			//                            return arg.ToString().ToUpper().StartsWith(filterBox.Text.ToUpper());
			//                        };
		}
	}
}
